package domfarr.files;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

public class FunWithFilesTest {

	private static final String ROOT_DIRECTORY = "C:/Projects/IC2008/FullList/";
	@SuppressWarnings("unused")
	private static final String XLS_TYPE = "xls";
	@SuppressWarnings("unused")
	private static final String GIF_TYPE = "gif";

	public static void main(String[] args) {
		FunWithFiles funWithFiles = new FunWithFiles(ROOT_DIRECTORY);
		Collection<File> files = funWithFiles.findAllFilesUptoMaxBytes(0);

		System.out.println(files);
	}

	static class FunWithFiles {
		private final String rootDirectory;
		private final static String ATTACHMENT_DIR = "att";

		public FunWithFiles(String rootDirectory) {
			this.rootDirectory = rootDirectory;
		}

		public int getCountOfAttachmentType(String extension) {
			File root = new File(this.rootDirectory);

			if (!root.isDirectory()) {
	            throw new IllegalArgumentException(
	                    "Parameter 'directory' is not a directory");
	        }

			int count = 0;
			for (File directory : root.listFiles()) {
				if (directory.isDirectory()) {
					File articleDir = new File(directory.getAbsolutePath() + File.separator + ATTACHMENT_DIR);
					if (articleDir.canExecute()) {
						for (File attachment : articleDir.listFiles()) {
							if (attachment.getName().toLowerCase().endsWith(extension)) {
								count++;
							}
						}
					}
				}
			}

			return count;
		}

		public Collection<File> findAllFilesUptoMaxBytes(int fileSizeInBytes) {
			File root = new File(this.rootDirectory);

			if (!root.isDirectory()) {
	            throw new IllegalArgumentException(
	                    "Parameter 'directory' is not a directory");
	        }

			Collection<File> files = new ArrayList<File>();
			innerListFiles(files, root, fileSizeInBytes);

			return files;
		}

	    private void innerListFiles(Collection<File> files, File directory, int fileSizeInBytes) {
	        File[] found = directory.listFiles();
	        if (found != null) {
	            for (int i = 0; i < found.length; i++) {
	                if (found[i].isDirectory()) {
	                	System.out.println(found[i].getAbsolutePath());
	                    innerListFiles(files, found[i], 0);
	                } else {
	                	if( found[i].length() <= fileSizeInBytes && (!found[i].getName().endsWith("html"))) {
	                        files.add(found[i]);
	                	}
	                }
	            }
	        }
	    }
	}
}
